package czy.demo.service;

import czy.demo.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 使用jdbc的用户详情服务,用户详情对象UserDetails在Principal对象中返回
 * 继承了JdbcDaoImpl实现,使用不同的sql语句查询
 */
@Component
public class JdbcUserDetailsService extends JdbcDaoImpl{

    private static final Logger logger = LoggerFactory.getLogger(JdbcUserDetailsService.class);

    private static final String USERS_QUERY = "select username,password,age,enabled from users " + "where username = ?";

    @Autowired
    private JdbcTemplate template;

    @Override
    protected void checkDaoConfig() {
    }

    @Override
    protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
        return template;
    }

    /**
     * 内部的加载用户信息的方法,覆盖它,其他方法采用默认,加入了自己的用户属性
     * @param username:用户名
     * @return:自己实现的User列表
     */
    @Override
    protected List<UserDetails> loadUsersByUsername(String username) {
        return this.template.query(USERS_QUERY,
                new String[] { username }, new RowMapper<UserDetails>() {
                    @Override
                    public UserDetails mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        String username = rs.getString(1);
                        String password = rs.getString(2);
                        Integer age = rs.getInt(3);
                        boolean enabled = rs.getBoolean(3);
                        return new User(username, password, enabled, true, true, true,
                                AuthorityUtils.NO_AUTHORITIES,age);
                    }

                });
    }

    /**
     * 内部的加载权限的方法
     * @param username
     * @return
     */
    @Override
    public List<GrantedAuthority> loadUserAuthorities(String username) {
        return this.template.query(JdbcDaoImpl.DEF_AUTHORITIES_BY_USERNAME_QUERY,
                new String[] { username }, new RowMapper<GrantedAuthority>() {
                    @Override
                    public GrantedAuthority mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        String roleName = "" + rs.getString(2);
                        return new SimpleGrantedAuthority(roleName);
                    }
                });
    }

    /**
     * 加载组权限的方法
     * @param username
     * @return
     */
    @Override
    protected List<GrantedAuthority> loadGroupAuthorities(String username) {
        return this.template.query(JdbcDaoImpl.DEF_GROUP_AUTHORITIES_BY_USERNAME_QUERY,
                new String[] { username }, new RowMapper<GrantedAuthority>() {
                    @Override
                    public GrantedAuthority mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        String roleName = getRolePrefix() + rs.getString(3);

                        return new SimpleGrantedAuthority(roleName);
                    }
                });
    }


}
